﻿using Sdg.Domain;
using Sdg.Domain.BizObj;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sdg.Domain.Services
{
    public class Director
    {
        List<IView> _views = new List<IView>();
        private IScriptBuilder _scriptBild;
        private BizObj.Connection _conn;
        private Dictionary<string, string> _scriptCache = new Dictionary<string, string>();
        private List<TableSettings> _tables = new List<TableSettings>();
        private List<BizObj.IBizObj> _bizObjects = new List<IBizObj>();
        private IViewManager _viewMAnager;


        public Director()
        {

        }

        /*public Director(IScriptBuilder scriptBild)
        {
            this._scriptBild = scriptBild;
             this._conn = conn;
             _views.AddRange(views);
        }*/

        public Director(IViewManager viewManager)
        {
            _viewMAnager = viewManager;
        }

        public void SetScriptBuilder(IScriptBuilder scriptBuilder)
        {
            _scriptBild = scriptBuilder;
        }

        private IView[] Views
        {
            get
            {
                return _views.ToArray();
            }
        }

        public void AddView(IView view)
        {
            _views.Add(view);
            view.Done += view_Done;
        }

        private void NotifyViews(ViewEventArgs args)
        {
            if (_views != null && _views.Count > 0)
                foreach (var item in _views)
                {
                    item.Notify(args);
                }
        }

        void view_Done(object sender, ViewEventArgs e)
        {
            HandleViewDoneAction(e);
        }

        private void HandleViewDoneAction(ViewEventArgs e)
        {
            var bizObj = e.View.BizObject;

            StoreBizObject(bizObj);

            e.Handled = SetConnection(bizObj);

            if (!e.Handled)
                HandleViewDoneAction(e);
        }

        private bool SetConnection(IBizObj bizObj)
        {
            if (bizObj is Connection)
            {
                _conn = bizObj as Connection;
                NotifyViews(new ViewEventArgs(null, bizObj));
                return true;
            }

            return false;
        }

        private void StoreBizObject(IBizObj bizObj)
        {
            if (bizObj != null)
                if (_bizObjects.Contains(bizObj))
                {
                    _bizObjects.Remove(bizObj);
                    _bizObjects.Add(bizObj);
                }
        }

        public void RemoveView(IView view)
        {
            if (_views.Contains(view))
                _views.Remove(view);
        }

        public string[] GetScript()
        {
            if (_scriptBild == null)
                throw new NullReferenceException("Не установлен сборщик скрипта");

            return null;
        }

        private string GetScriptForTable(TableSettings table, bool refresh = false)
        {

            if (_scriptCache.ContainsKey(table.Name))
            {
                if (string.IsNullOrWhiteSpace(_scriptCache[table.Name]) || refresh)
                {
                    _scriptBild.SetTable(table);
                    _scriptCache[table.Name] = _scriptBild.GetScript();
                    return _scriptCache[table.Name];
                }

                return _scriptCache[table.Name];
            }

            _scriptCache.Add(table.Name, _scriptBild.GetScript());
            return _scriptCache[table.Name];
        }        

        public void InitViews()
        {
            _viewMAnager.InitializeViews(this);
        }
    }
}
